/**   
 * @title TemplateServiceImpl.java 
 * @package com.hopechart.importutil.model.service.impl 
 * @description 业务成接口实现
 * @author daijiang@hopechart.com
 * @date 2016年1月13日 上午8:50:14 
 * @version v1.0.0
 * @copyright 杭州鸿泉数字设备有限公司
 */
package com.hopechart.importutil.model.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jodd.util.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hopechart.importutil.dao.impl.TemplateDaoImpl;
import com.hopechart.importutil.exception.BusinessException;
import com.hopechart.importutil.model.pojo.TemplateEntity;
import com.hopechart.importutil.model.service.ITemplateService;
import com.hopechart.importutil.util.ExcelUtil;
import com.hopechart.importutil.util.FileUtil;
import com.hopechart.importutil.util.PropertiesUtil;

/** 
 * @className TemplateServiceImpl 
 * @description TODO 
 * @author daijiang@hopechart.com
 * @date 2016年1月13日 上午8:50:14 
 *  
 */
public class TemplateServiceImpl implements ITemplateService {

    private static final Logger log = LoggerFactory.getLogger(TemplateServiceImpl.class);
    
    @Autowired
    private TemplateDaoImpl templateDaoImpl; 
    
    private List<File> getImportFile() throws BusinessException {
        return FileUtil.getDirFiles(PropertiesUtil.getValue("data.file.path"));
    }
    
    public List<TemplateEntity> file2Code(File file) {
        if(null == file) {
            throw new BusinessException("文件对象为空，无法进行转换！");
        }
        List<TemplateEntity> rs = new ArrayList<TemplateEntity>();
        try {
            rs = ExcelUtil.importExcelData(
                    TemplateEntity.class, FileUtil.getFileStream(file),
                    getCols(), FileUtil.getExcelFileType(file));
        } catch(BusinessException e) {
            throw new BusinessException("文件【" + file.getName() + "】导入异常！", e);
        }
        return rs;
    }
    
    public List<List<TemplateEntity>> files2Codes(List<File> files) {
        List<List<TemplateEntity>> codes = null;
        if(null != files) {
            for (File file : files) {
                try {
                    log.info("当前处理文件：" + file.getName());
                    codes.add(file2Code(file));
                } catch(BusinessException e) {
                    log.info(e.getMessage());
                    log.error(e.getMessage(), e);
                }
            }
        }
        return codes;
    }

    @Override
    public int batchInsert() throws BusinessException{
        List<File> files = getImportFile();
        int fileCount = 0;
        int recordCount = 0;
        for (File file : files) {
            log.info("当前导入文件：" + file.getName());
            List<TemplateEntity> codes = file2Code(file);
            log.info("文件【" + file.getName() + "】共产生" + codes.size() +"条记录！");
            
            recordCount += templateDaoImpl.batchInsert(codes);
            fileCount++;
        }
        log.info("共处理文件【" + fileCount + "】个，插入记录【" + recordCount + "】条！");
        return fileCount;
    }
    
    public TemplateDaoImpl getCodeTableDao() {
        return templateDaoImpl;
    }

    public void setCodeTableDao(TemplateDaoImpl codeTableDao) {
        this.templateDaoImpl = codeTableDao;
    }

    private String[] getCols() {
        String colsString = PropertiesUtil.getValue("bean.attr");
        if(StringUtil.isEmpty(colsString)) {
            return new String[] {};
        } else {
            return colsString.trim().split(",");
        }
    }
    
}
